
#include <QFile>
#include "qlogger_.hpp"
#include "qloggerappender.hpp"
#include "qloggerengine.hpp"

int QLoggerEngine::elapsed ()
{
    return handle().startTime.elapsed();
}

QLogger * QLoggerEngine::getLogger (const QString &moduleName)
{
    QLogger *logger;
    logger = handle().loggers[moduleName];
    if (logger == NULL)
        {
            logger = new QLogger(moduleName);
            handle().loggers[moduleName] = logger;
        }
    return logger;
}

const char * QLoggerEngine::levelName (QLoggerLevel level)
{
    switch (level)
        {
        case LevelDebug:     return "Debug";
        case LevelInfo:      return "Info";
        case LevelWarn:      return "Warn";
        case LevelError:     return "Error";
        case LevelCritical:  return "Critical";
        case LevelFatal:     return "Fatal";
        case LevelException: return "Exception";
        default:
            return "Unknown";
        }
}

bool QLoggerEngine::hasAppender(QLoggerAppender* a) const
{
    for(int i = 0; i < appenders_.size(); ++i)
        {
            if(appenders_.at(i).data() == a)
                return true;
        }
    return false;
}

QSharedPointer<QLoggerAppender> QLoggerEngine::appender(QLoggerAppender* a)
{
    QSharedPointer<QLoggerAppender> res;
    if(hasAppender(a))
        {
            for(int i = 0; i < appenders_.size(); ++i)
                {
                    if(appenders_.at(i).data() == a)
                        {
                            res = appenders_[i];
                            return res;
                        }
                }
        }
    else
        {
            res = QSharedPointer<QLoggerAppender>(a);
            appenders_.append(res);
        }
    return res;
}

QLoggerEngine::~QLoggerEngine ()
{
    foreach (QLogger *logger, loggers)
        {
            delete logger;
        }
}

